﻿namespace MineSweeping
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;

    public partial class Block
    {
        private static readonly IDictionary<int, SolidColorBrush> brushes = new Dictionary<int, SolidColorBrush>();

        private static readonly ImageBrush flagBrush = new ImageBrush
                                                           {
                                                               ImageSource =
                                                                   new BitmapImage(new Uri(
                                                                                       "/Picture/56_block_flag.png",
                                                                                       UriKind.Relative))
                                                           };

        private static readonly ImageBrush blockBrush = new ImageBrush
                                                            {
                                                                ImageSource =
                                                                    new BitmapImage(new Uri("/Picture/56_block.png",
                                                                                            UriKind.Relative))
                                                            };

        private static readonly ImageBrush radMineBrush = new ImageBrush
                                                              {
                                                                  ImageSource =
                                                                      new BitmapImage(new Uri("/Picture/56_red_mine.png",
                                                                                              UriKind.Relative))
                                                              };

        public int Row { get; private set; }

        public int Column { get; private set; }

        public int MineValue { get; private set; }

        public bool Flaged { get; set; }

        public bool Showed { get; set; }

        static Block()
        {
            brushes.Add(-1, new SolidColorBrush(Colors.Gray));
            brushes.Add(0, new SolidColorBrush(Colors.White));
            brushes.Add(1, new SolidColorBrush(Colors.Blue));
            brushes.Add(2, new SolidColorBrush(Colors.Green));
            brushes.Add(3, new SolidColorBrush(Colors.Brown));
            brushes.Add(4, new SolidColorBrush(Colors.Orange));
            brushes.Add(5, new SolidColorBrush(Colors.Purple));
            brushes.Add(6, new SolidColorBrush(Colors.Magenta));
            brushes.Add(7, new SolidColorBrush(Colors.Cyan));
            brushes.Add(8, new SolidColorBrush(Colors.Red));
        }

        public Block(int row, int column, int blockEdge, int mineValue)
        {
            InitializeComponent();
            BackGrid.Background = blockBrush;

            this.Row = row;
            this.Column = column;
            this.Height = blockEdge;
            this.Width = blockEdge;
            this.MineValue = mineValue;
        }

        public void SetShowed(int value)
        {
            if (value == 9)
            {
                BackGrid.Background = radMineBrush;
            }
            else
            {
                BackGrid.Background = brushes[0];
                this.Value.Foreground = brushes[value];
                this.Value.Text = value.ToString(CultureInfo.InvariantCulture);
            }
        }

        public int SetFlag()
        {
            int changendValue;

            if (Flaged)
            {
                BackGrid.Background = blockBrush;
                changendValue = 1;
            }
            else
            {
                BackGrid.Background = flagBrush;
                changendValue = -1;
            }
            Flaged = !Flaged;

            return changendValue;
        }
    }
}
